package ywh.player.media
{
	import com.yahoo.astra.fl.controls.Tree;
	import com.yahoo.astra.fl.controls.treeClasses.TNode;
	import fl.core.UIComponent;
	import fl.events.ListEvent;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import ywh.list.CourseItem;
	import ywh.manager.Sync;
	import ywh.manager.Tag;
	import ywh.player.AbstractShower;
	import ywh.ui.MyTreeCellRenderer;
	import ywh.ui.TreeMenu;
	import ywh.ui.TreeMenuDataProvider;
	
	/**
	 * 树状菜单类;
	 * @author WilliWHYang
	 */
	public class TreeShower extends AbstractShower
	{
		private var _currentNode:TNode;
		private var _treeMenu:TreeMenu;
		private var _startTimeName:String;
		private var _stage:Stage;
		
		public function TreeShower()
		{
			_startTimeName = Tag._START_TIME.substring(1);
		}
		
		override public function set dataProvider(value:XML):void
		{
			_treeMenu.dataProvider = new TreeMenuDataProvider(value);
		}
		
		override protected function initDisplayer():void
		{
			
			if (render)
			{
				_treeMenu = render as TreeMenu;
			}
			else
			{
				render = _treeMenu = new TreeMenu();
			}
			_treeMenu.rowHeight = 30;
			_treeMenu.addEventListener(ListEvent.ITEM_CLICK, onNodeClick);
		}
		
		override public function set playItem(item:CourseItem):void
		{
			super.playItem = item;
			var foundNode:TNode = _treeMenu.findNode(_startTimeName, item[_startTimeName] + '');
			var nodeIndex:uint = _treeMenu.showNode(foundNode);
			_treeMenu.selectedIndex = nodeIndex;
			_treeMenu.scrollToSelected();
			_currentNode = foundNode;
		}
		
		private function onNodeClick(treeEvent:ListEvent):void
		{
			
			var item:Object = treeEvent.item;
			var time:Number = item.hasOwnProperty(_startTimeName) ? Number(item[_startTimeName]) : NaN;
			if (!isNaN(time))
			{
				Sync.time = time;
				_container.parent.removeChild(_container);
			}
			//防止树状菜单的选中状态;
			treeEvent.preventDefault();
		}
	
//		private function checkIcon(tnode:TNode):String
//		{
//			if (tnode[_status_Name] == 1)
//			{
//				if (tnode is BranchNode)
//				{
//					if ((tnode as BranchNode).nodeState == TreeDataProvider.OPEN_NODE)
//					{
//						return "icon_brunch_open_f";
//					}
//					else
//					{
//						return "icon_brunch_close_f";
//					}
//				}
//				else
//				{
//					return "icon_leaf_f";
//				}
//			}
//			else
//			{
//				if (tnode is BranchNode)
//				{
//					if ((tnode as BranchNode).nodeState == TreeDataProvider.OPEN_NODE)
//					{
//						return "icon_brunch_open";
//					}
//					else
//					{
//						return "icon_brunch_close";
//					}
//				}
//				else
//				{
//					return "icon_leaf";
//				}
//			}
//		}
//		
//		public function setNodeStatus(status:Number, tnode:TNode):void
//		{
//			status = (status > 1) ? 1 : ((status < 0 || isNaN(status)) ? 0 : status);
//			tnode[_status_Name] = status;
//			//_treeMenu.setRendererStyle("iconField", checkIcon(tnode), _treeMenu.dataProvider.getItemIndex(tnode));
//			if (tnode.parentNode)
//			{
//				var childeArray:Array = tnode.parentNode.children;
//				var parentStatus:Number = 0;
//				for each (var childeNode:Object in childeArray)
//				{
//					parentStatus += childeNode[_status_Name];
//				}
//				parentStatus = parentStatus / childeArray.length;
//				setNodeStatus(parentStatus, tnode.parentNode);
//			}
//			return;
//		}
//		
//		private function checkNodeStatus(tnode:TNode):void
//		{
//			var starter:uint = tnode[_start_clip_index];
//			var foundNode:TNode = _treeMenu.findNode(_index, String(Number(tnode[_index]) + 1));
//			var ender:uint = foundNode ? foundNode[_start_clip_index] : Course.C.mainstream.amount() - 1;
//			ender = (ender == starter) ? ender + 1 : ender;
//			var statusNum:Number = 0;
//			var cont:uint = 0;
//			do
//			{
//				cont++;
//				statusNum += ScormRecoder.SCORM_OBJ.getItemStatus(starter);
//				starter++;
//			} while (starter < ender);
//			setNodeStatus(statusNum / cont, tnode);
//		}
	
	}

}